using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class ProductsControl
    {
        #region Local Variable
        private ProductsDataAccess _objDAO;
        #endregion Local Variable

        #region Method
        public ProductsControl()
        {
            _objDAO = new ProductsDataAccess();
        }

        public ProductsInfo Get(
        Int64 ID,
        ref string sErr)
        {
            return _objDAO.Get(
            ID,
            ref sErr);
        }

        public DataTable GetAll(
        ref string sErr)
        {
            return _objDAO.GetAll(
            ref sErr);
        }
        public DataTable GetByPos(
        int pos, ref string sErr)
        {
            return _objDAO.GetByPos(
            pos, ref sErr);
        }
        public int GetCountRecord(
        ref string sErr)
        {
            return _objDAO.GetCountRecord(
            ref sErr);
        }

        public Int32 Add(ProductsInfo obj, ref string sErr)
        {
            return _objDAO.Add(obj, ref sErr);
        }

        public string Update(ProductsInfo obj)
        {
            return _objDAO.Update(obj);
        }

        public string Delete(
        Int64 ID
        )
        {
            return _objDAO.Delete(
            ID
            );
        }
        public Boolean IsExist(
        Int64 ID
        )
        {
            return _objDAO.IsExist(
            ID
            );
        }

        public DataTableCollection Get_Page(ProductsInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
        {
            return _objDAO.Get_Page(obj, orderBy, pageIndex, pageSize, ref sErr);
        }

        public DataTable Search(String columnName, String columnValue, String condition, String tableName, ref String sErr)
        {
            return _objDAO.Search(columnName, columnValue, condition, tableName, ref  sErr);
        }
        public string InsertUpdate(ProductsInfo obj)
        {
            string sErr = "";
            if (IsExist(
            obj.ID
            ))
            {
                sErr = Update(obj);
            }
            else
                Add(obj, ref sErr);
            return sErr;
        }

        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            return _objDAO.GetTransferOut(dtb, from, to, ref sErr);
        }

        public DataTable ToTransferInStruct()
        {
            return ProductsInfo.ToDataTable();
        }

        public string TransferIn(DataRow row)
        {
            ProductsInfo inf = new ProductsInfo(row);
            return InsertUpdate(inf);
        }
        #endregion Method
        public DataTable GetByCate(int cate, ref string sErr)
        {
            return _objDAO.GetByCate(cate, ref sErr);
        }
        public DataTable GetByArrCate(string arrCate, ref string sErr)
        {
            string xmlCate = ParseXML(arrCate);
            return GetByArrCate(xmlCate, "/ROOT/ITEM", ref sErr);
        }

        private static string ParseXML(string arrCate)
        {
            string[] arrTmp = arrCate.Split(',');
            string xmlCate = "<ROOT>";
            for (int i = 0; i < arrTmp.Length; i++)
            {
                xmlCate += "<ITEM ID =\"" + arrTmp[i] + "\"></ITEM>";
            }
            xmlCate += "</ROOT>";
            return xmlCate;
        }
        public DataTable GetByArrCate(string xmlCate, string xmlPath, ref string sErr)
        {
            return _objDAO.GetByArrCate(xmlCate, xmlPath, ref sErr);
        }
        public DataTable get_allproduct(DataTable dt, ref string sErr)
        {
            return _objDAO.get_allproduct(dt, ref sErr);
        }

        public string GetIDByArrCate(string arrCate, ref string sErr)
        {
            string xmlCate = ParseXML(arrCate);
            List<string> result = new List<string>();
            DataTable dt = _objDAO.GetIDByArrCate(xmlCate, "/ROOT/ITEM", ref sErr);
            foreach (DataRow row in dt.Rows)
            {
                if (!result.Contains(row["ID"].ToString()))
                    result.Add(row["ID"].ToString());
            }
            return string.Join(",", result.ToArray());
        }

        public string GetIDByArrCate(string arrid, string arrCate, ref string sErr)
        {
            string xmlCate = ParseXML(arrCate);
            string xmlID = ParseXML(arrid);
            List<string> result = new List<string>();
            DataTable dt = _objDAO.GetIDByArrCate(xmlID, xmlCate, "/ROOT/ITEM", ref sErr);
            foreach (DataRow row in dt.Rows)
            {
                if (!result.Contains(row["ID"].ToString()))
                    result.Add(row["ID"].ToString());
            }
            return string.Join(",", result.ToArray());
        }

        public DataTable GetInID(string arrid, ref string sErr)
        {
            string xmlID = ParseXML(arrid);
            return _objDAO.GetInID(xmlID, "/ROOT/ITEM", ref  sErr);
        }

        public DataTable LoadCart(List<CartInfo> lcart, ref string sErr)
        {
            string arrid = "";
            foreach (CartInfo x in lcart)
            {
                arrid += "," + x.ProductID;
            }
            if (arrid.Length > 0)
                arrid = arrid.Substring(1);
            string xmlID = ParseXML(arrid);
            DataTable dt = _objDAO.GetInID(xmlID, "/ROOT/ITEM", ref  sErr);
            dt.Columns.Add("Qty", typeof(int));
            if (dt.Rows.Count > 0)
                foreach (CartInfo x in lcart)
                {
                    DataRow[] arrRow = dt.Select("ID=" + x.ProductID);
                    if (arrRow.Length > 0)
                        arrRow[0]["Qty"] = x.Qty;
                }
            dt.Columns.Add("SubTotal", typeof(double), "NewPrice*Qty");
            return dt;
        }

        public DataTable GetTopSeller(ref string sErr)
        {
            return _objDAO.GetTopSeller(ref sErr);
        }

        public DataTable GetTopNew(ref string sErr)
        {
            return _objDAO.GetTopNew(ref sErr);
        }

        public DataTable GetAllIndex(ref string sErr)
        {
            return _objDAO.GetAllIndex(ref sErr);
        }
    }
}
